package com.example.dell.fastshopping.user;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.Trade.TradeExtraclActivity;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.user.entity.Balance;
import com.example.dell.fastshopping.user.modle.MoneyActivityMolder;
import com.example.dell.fastshopping.utils.DateUtils;
import com.example.dell.fastshopping.views.BaseListActivity;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MoneyActivity extends BaseListActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.nodata)
    LinearLayout nodata;
    @BindView(R.id.refresh)
    TwinklingRefreshLayout refresh;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.extrack)
    TextView extrack;
    private MoneyActivityMolder molder;

    private List<Balance> mList;
    private String balance;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money);
        ButterKnife.bind(this);
        super.initData();
        initView();
    }

    @Override
    protected TwinklingRefreshLayout getRefreshLayout() {
        return refresh;
    }

    private void initView() {
        title.setText("余额");
        molder = ViewModelProviders.of(this).get(MoneyActivityMolder.class);
        Intent intent = getIntent();
        balance = intent.getStringExtra("balance");
        money.setText(balance);

        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        getNetData(1, 15);
    }

    @Override
    protected void getNetData(final int page, int pageSize) {
        molder.getBalance(page, pageSize).observe(this, new Observer<List<Balance>>() {
            @Override
            public void onChanged(@Nullable List<Balance> list) {
                //TODO:
                if (page == 1) {
                    mList = list;
                } else {
                    mList.addAll(list);
                }
                loadFinsh();
                if (mList.size() == 0) {
                    nodata.setVisibility(View.VISIBLE);
                } else {
                    nodata.setVisibility(View.GONE);
                }
                RecyclerView.Adapter adapter = rv.getAdapter();
                if (adapter == null) {
                    rv.setAdapter(new MyAdapter());
                } else {
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.extrack})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.extrack:
                toBanlanceActivity();
                break;
        }
    }

    //提现
    private void toBanlanceActivity() {

        Intent intent = new Intent(this, TradeExtraclActivity.class);
        intent.putExtra("money", balance);
        intent.putExtra("from", "money");
        startActivity(intent);

    }

    private class MyAdapter extends RecyclerView.Adapter<MyHoldle> {
        @NonNull
        @Override
        public MyHoldle onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(MoneyActivity.this).inflate(R.layout.item_integral, viewGroup, false);
            MyHoldle myHoldle = new MyHoldle(inflate);
            return myHoldle;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHoldle myHoldle, int i) {
            Balance balance = mList.get(i);
            myHoldle.type.setText(balance.getChannelName());
            myHoldle.time.setText(DateUtils.stampToDate(balance.getCreateTime()));
            myHoldle.num.setText(balance.getLimitnoString());
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    private class MyHoldle extends RecyclerView.ViewHolder {

        public TextView type;
        public TextView time;
        public TextView num;

        public MyHoldle(View inflate) {
            super(inflate);
            type = inflate.findViewById(R.id.jifen_type);
            time = inflate.findViewById(R.id.time);
            num = inflate.findViewById(R.id.num);
        }
    }
}
